// 源插件
import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
import router from 'umi/router';
import { NumberDivide, DateDiff, FormatnumberZero, Mover, DateStamp } from '@/function.js';

// 视图插件
import {
  BackTop,
  Empty,
  Spin,
  Row,
  Col,
  Card,
  Form,
  Input,
  Select,
  Icon,
  Button,
  Dropdown,
  Menu,
  InputNumber,
  DatePicker,
  Modal,
  message,
  Badge,
  Divider,
  Steps,
  Radio,
  Upload,
} from 'antd';

// 外部组件
import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import Loading from '@/components/Loading/Loading';

// css OR less预编译器
import styles from './All.less';

// 图片

/**
 *  Describe:数据连接
 *  Created by xjc on 2019/5/15
 */

@connect(({ cpb, loading }) => ({
  cpb,
  loading: loading.models.cqbRule,
}))
@Form.create()
class Option extends PureComponent {
  componentWillMount() {}

  /**
   *  Describe:当前状态
   *  Created by xjc on 2019/5/15
   */

  state = {
    listLoading: false, // 加载状态
    selectedRows: [], // 选择行
    formValues: {
      num: 10,
      page: 1,
    }, // 表格参数
    /* pageValues:{current: 1, pageSize: 10, total: 0}, */
    StocksDetailVisible: false, // modal开关
    OptionVisible: false, // modal开关
    record: {}, // 当前行内容
    searchText: '', // 搜索内容
    columns: [], // 表头处理结果
    currentDataSource: [], // 表剩余数据
  };

  /**
   *  Describe:本地搜索
   *  Created by xjc on 2019/6/4
   */

  getColumnSearchProps = dataIndex => ({
    filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
      <div style={{ padding: 8 }}>
        <Input
          ref={node => {
            this.searchInput = node;
          }}
          placeholder={`Search ${dataIndex}`}
          value={selectedKeys[0]}
          onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
          onPressEnter={() => this.handleSearch(selectedKeys, confirm)}
          style={{ width: 188, marginBottom: 8, display: 'block' }}
        />
        <Button
          type="primary"
          onClick={() => this.handleSearch(selectedKeys, confirm)}
          icon="search"
          size="small"
          style={{ width: 80, marginRight: 8 }}
        >
          搜索
        </Button>
        <Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 80 }}>
          重置
        </Button>
      </div>
    ),
    filterIcon: filtered => (
      <Icon type="search" style={{ color: filtered ? '#1880ff' : '#181816', fontWeight: '700' }} />
    ),
    onFilter: (value, record) => {
      if (record[dataIndex] === '股票合计') {
        return true;
      }
      return record[dataIndex] === null
        ? ''
        : record[dataIndex]
            .toString()
            .toLowerCase()
            .includes(value.toLowerCase());
    },
    onFilterDropdownVisibleChange: visible => {
      if (visible) {
        setTimeout(() => this.searchInput.select());
      }
    },
    render: text => <span>{text}</span>,
  });

  handleSearch = (selectedKeys, confirm) => {
    confirm();
    this.setState({ searchText: selectedKeys[0] });
  };

  handleReset = clearFilters => {
    clearFilters();
    this.setState({ searchText: '' });
  };

  getColumns() {
    const {
      cpb: { stocksFilters },
    } = this.props;
    return [
      {
        title: '编号',
        dataIndex: 'id',
        align: 'center',
        width: 60,
      },
      {
        title: '交易',
        dataIndex: 'type',
        width: 60,
        align: 'center',
        filters: [
          {
            text: 'sp',
            value: '1',
          },
          {
            text: 'sc',
            value: '2',
          },
          {
            text: 'bp',
            value: '3',
          },
          {
            text: 'bc',
            value: '4',
          },
        ],
        onFilter: (value, record) => {
          if (record.type !== undefined) {
            return record.type.toString().indexOf(value) === 0;
          }
        },
        render: (value, row, index) => {
          return (
            <span>
              {value === 1
                ? 'sp'
                : value === 2
                ? 'sc'
                : value === 3
                ? 'bp'
                : value === 4
                ? 'bc'
                : null}
            </span>
          );
        },
      },
      {
        title: '挂勾',
        dataIndex: 'stock',
        width: 90,
        align: 'center',
        ...this.getColumnSearchProps('stock'),
      },
      {
        title: '行使价',
        dataIndex: 'strikeprice',
        width: 80,
        align: 'center',
        sorter: (a, b) => a.stocknumber - b.stocknumber,
      },
      {
        title: '股数',
        dataIndex: 'stocknumber',
        align: 'center',
        width: 80,
        sorter: (a, b) => a.stocknumber - b.stocknumber,
      },
      {
        title: '交易日',
        width: 80,
        dataIndex: 'tradedate',
        align: 'center',
        sorter: (a, b) => DateStamp(a.tradedate) - DateStamp(b.tradedate),
        render: (value, row, index) => {
          return <span>{typeof value === 'string' ? value.split(' ')[0] : ''}</span>;
        },
      },
      {
        title: '到期日',
        width: 80,
        dataIndex: 'expriy',
        align: 'center',
        sorter: (a, b) => DateStamp(a.expriy) - DateStamp(b.expriy),
        render: (value, row, index) => {
          return <span>{typeof value === 'string' ? value.split(' ')[0] : ''}</span>;
        },
      },
      {
        title: '现价',
        width: 80,
        dataIndex: 'stockprice',
        align: 'center',
        sorter: (a, b) => a.stockprice - b.stockprice,
      },
      {
        title: '交易价格',
        width: 80,
        dataIndex: 'stockmarketprice',
        align: 'center',
        sorter: (a, b) => a.stockmarketprice - b.stockmarketprice,
      },
      {
        title: '距离行使价',
        width: 80,
        dataIndex: 'pricerate',
        align: 'center',
        sorter: (a, b) => a.pricerate - b.pricerate,
        render: (value, row, index) => {
          return <span>{typeof value === 'number' ? `${(value * 100).toFixed(2)}%` : ''}</span>;
        },
      },
      {
        title: '期权金收入',
        width: 80,
        dataIndex: 'premiumrate',
        align: 'center',
        sorter: (a, b) => a.premiumrate - b.premiumrate,
        render: (value, row, index) => {
          return <span>{typeof value === 'number' ? `${(value * 100).toFixed(2)}%` : ''}</span>;
        },
      },
      {
        title: '绝对金额',
        width: 80,
        dataIndex: 'absoluteamount',
        align: 'center',
        sorter: (a, b) => a.absoluteamount - b.absoluteamount,
      },
      {
        title: '收益',
        width: 80,
        dataIndex: 'proceedrate',
        align: 'center',
        sorter: (a, b) => a.proceedrate - b.proceedrate,
        render: (value, row, index) => {
          return <span>{typeof value === 'number' ? `${(value * 100).toFixed(2)}%` : ''}</span>;
        },
      },
      {
        title: '曝险部位(USD)',
        width: 80,
        dataIndex: 'explosivepart',
        align: 'center',
        sorter: (a, b) => a.explosivepart - b.explosivepart,
      },
    ];
  }

  handleStandardTableChange = (pagination, filtersArg, sorter, extra) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;
    this.setState({
      currentDataSource: extra.currentDataSource,
    });
    /* this.setState({
      currentDataSource: extra.currentDataSource
    });
    /!*导入列表数据*!/
    dispatch({
      type: 'cpb/GetStockCpbList',
      callback: (res) => {

      }
    }); */
  };

  /**
   *  Describe:视图
   *  Created by xjc on 2019/5/15
   */

  render() {
    const {
      loading,
      cpb: { options },
    } = this.props;
    const { listLoading, selectedRows, record, StocksDetailVisible, OptionVisible } = this.state;
    let columns = [];
    if (columns.length === 0 && options.code === 1) {
      columns = this.getColumns();
    }
    return (
      <PageHeaderWrapper title="花旗期权部分">
        <Card>
          {options.code === 0 ? (
            <Loading />
          ) : (
            <div>
              <div style={{ margin: 8, fontSize: '1rem' }}>
                共计：{this.state.currentDataSource.length}
              </div>
              <StandardTable
                bordered
                selectedRows={selectedRows}
                loading={listLoading}
                data={options}
                columns={columns}
                style={{ tableLayout: 'fixed' }}
                className={styles.TableLayout}
                pagination={false}
                onChange={this.handleStandardTableChange}
                scroll={{ x: 1400, y: 618 }}
                /* rowClassName={(record, index) =>
                     record.stock === '股票合计'? styles.statisticssTableBackground : styles.ffffff
                   } */
                // rowKey="id"
              />
            </div>
          )}
        </Card>
      </PageHeaderWrapper>
    );
  }

  componentDidMount() {
    const { dispatch } = this.props;

    /* 导入列表数据 */
    dispatch({
      type: 'cpb/GetOptionCpbLis',
      callback: res => {
        this.setState({
          currentDataSource: res.list,
        });
      },
    });
  }

  componentWillUnmount() {}
}

export default Option;
